package com.fet.refreshOffer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fet.refreshOffer.impl.HttpHandlerHttpClientImpl;

public class RefreshOfferMain {
	
	public static final String REFRESH_OFFER = "refreshOffer";
	public static final String REFRESH_DB = "refreshDB";
	
	public static final int PROMOTION_PLAN = 2;
	
	/*
	public static final int PRICE_PLAN = 1;
	public static final int VAS = 3;
	public static final int DISCOUNT = 4;
	public static final int LOYALTY = 5;
	public static final int VASCONTRACT = 6;
	public static final int NONBILLING = 7;
	public static final int DETAIL_STATUS = 0;
	*/
	
	Log log = LogFactory.getLog(HttpHandlerHttpClientImpl.class);
	
	public static String[] getUrlListFromString(String urlString){
		String[] urls = null;
		if(urlString != null && !"".equals(urlString))
			urls = urlString.split("\\;");
		return urls;
	}
	
	public static void appendUrlArrayToList(String[] urls,List<String> urlList){
		if(urlList == null)
			return;
		if(urls != null && urls.length > 0)
			for(String url:urls)
				if(!"".equals(url))
					urlList.add(url);
	}
	
	private List<String> getUrlList(String action) throws IOException{
		File configFile = new File("CommonServiceClient.properties");
		Properties properties = new Properties();
		properties.load(new FileInputStream(configFile));
		String containerUrlString = properties.getProperty("REFRESH_OFFER_CONTAINER");
		String presentationUrlString = properties.getProperty("REFRESH_OFFER_PRESENTATION");
		String[] containerUrls = getUrlListFromString(containerUrlString);
		String[] presentationUrls = getUrlListFromString(presentationUrlString);
		
		List<String> urlList = new ArrayList<String>();
		if(REFRESH_DB.equals(action)){
			if(containerUrls != null && containerUrls.length > 0)
				for(String url:containerUrls)
					if(!"".equals(url)){
						urlList.add(url);
						break;
					}
		}else{
			appendUrlArrayToList(containerUrls,urlList);
			appendUrlArrayToList(presentationUrls,urlList);
		}
		return urlList;
	}
	
	public void RefreshOffer(String[] args ){

		try{
			String action = REFRESH_OFFER;
			String offerType = null;
			if(args != null && args.length > 0){
				if("1".equals(args[0].trim()))
					action = REFRESH_OFFER;
				else if("2".equals(args[0].trim()))
					action = REFRESH_DB;
				else
					throw new Exception("the first parameter for action must be 1(refreshOffer) or 2(refreshDB) !");
				if(args.length > 1 && "1".equals(args[1])){
					offerType = "" + PROMOTION_PLAN;
				}
			}
			
			List<String> urlList = getUrlList(action);
			
			HttpHandler httpClient = new HttpHandlerHttpClientImpl();
			Properties params = new Properties();
			params.setProperty("action", action);
			if(offerType != null && !"".equals(offerType))
				params.setProperty("offerType", offerType);
			if(urlList != null && urlList.size() > 0){
				log.info("start to " + action + " .... \n");
				for(String url:urlList){
					params.setProperty("temp", "" + Math.random());
					log.info("start a calling ,url=" + url);	
					try{
						httpClient.execMethod(url + "/refreshOffer", params, 0, 0);
					}catch(ConnectException e){
						log.error(e.getMessage());
					}
				}
			}
			log.info("refresh finished ... ");
		}catch(Exception e){
			log.error(e);
		}
	}
	
	public static void main(String[] args ) {
		new RefreshOfferMain().RefreshOffer(args);
	}
}







